package wsz.queue;

import com.alibaba.rocketmq.client.producer.DefaultMQProducer;
import com.alibaba.rocketmq.client.producer.SendResult;
import com.alibaba.rocketmq.common.message.Message;
import com.alibaba.rocketmq.common.message.MessageQueue;

import java.nio.charset.StandardCharsets;

/**
 * 生产者-消息发送到指定的主题的queue
 * @author wsz
 * @date 2022/2/24 15:13
 **/
public class Producer {

    public static void main(String[] args) throws Exception {
        DefaultMQProducer producer = new DefaultMQProducer("producer_grp_wsz");
        producer.setNamesrvAddr("192.168.174.138:9876");
        producer.start();

        // 消息
        Message message = new Message(
                "tp_queue_select",
                "select topic queue send message".getBytes(StandardCharsets.UTF_8)
        );
        /**
         * 负载均衡-指定发送的queue
         * sh mqbroker -p 获取broker name
         */
        SendResult send = producer.send(
                message,
                // 指定broker，topic中的queue
                new MessageQueue("tp_queue_select", "localhost.localdomain", 1),
                1000);
        // SendResult [sendStatus=SEND_OK, msgId=C0A8573142B418B4AAC2787DDC130000,offsetMsgId=C0A8AE8A00002A9F00000000000A4EFC, messageQueue=MessageQueue [topic=tp_queue_select, brokerName=localhost.localdomain, queueId=1], queueOffset=1]
        System.out.println(send);
        producer.shutdown();
    }
}
